using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.Presenters.Views;
using CMS.Presenters.Presenters;
using CMS.Web;
using CMS.Core.Domain;
using System.Collections.Generic;
using CMS.Data;

namespace CMS.Web.ListManager
{
    public partial class AddBaiViet : BaseUserControl, IAddBaiVietView
    {
        private AddBaiVietPresenter presenter;        
        private IList<BaiVietTheLoai> _TheLoaiList;
        bool isRole = false;
        NguoiSuDungInfo userInfo;
        Int32 MaChucNang = 0;

        public AddBaiViet()
        {
            this.presenter = new AddBaiVietPresenter(this, NHibernateDaoFactory.Instance.GetBaiVietDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtImage.Attributes.Add("onkeypress", "return false;");
            txtImage.Attributes.Add("onkeyup", "return false;");
            txtImage.Attributes.Add("onpaste", "return false;");
            
            txtNgayDang.Attributes.Add("readonly", "true");
            userInfo = (NguoiSuDungInfo)Session["UserInfo"];
            MaChucNang = this.FName;
            this.presenter.LoadTheLoais(MaChucNang);

            this.drpTheLoai.DataSource = this.TheLoaiList;
            this.drpTheLoai.DataBind();
            btnOK.Attributes.Add("onclick", "return validator();");
            this.txtNgayDang.Text = DateTime.Now.ToString("dd/MM/yyyy");
            this.chkState.Checked = true;
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            presenter.AddObj(Page.IsValid);           
            string strFile = CMS.Web.Files.Instance.UpLoadFile(this.txtImage.PostedFile, "");
            this.btnCancel_Click(this, null);
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=ADD");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW");
        }


        #region IAddBaiVietView Members

        public string Message
        {
            set 
            { 
                //lblMessage.Text = value;  
            }
        }

        public CMS.Core.Domain.BaiViet BaiVietToAdd
        {
            get 
            {
                BaiViet obj = new BaiViet();
                obj.TieuDe = this.txtTieuDe.Text.Trim();
                obj.AnhDaiDien = CMS.Web.Files.Instance.StandardName("", this.txtImage.PostedFile);
                obj.ChuThichAnh = this.txtImgDescr.Text.Trim();
                obj.TomTat = this.txtSum.Text.Trim();
                if (!txtNgayDang.Text.Trim().Equals(""))
                {
                    string[] temp = txtNgayDang.Text.Trim().Split(new Char[] { '/' });
                    obj.NgayCapNhat = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                }                
                obj.TrangThai = this.chkState.Checked;
                obj.NoiDung = this.txtBody.Value.Trim();
                obj.NguonTin = this.txtAnthor.Text.Trim();
                obj.MaTheLoaiLookup = new BaiVietTheLoaiDao().GetById(Convert.ToInt32(this.drpTheLoai.SelectedValue));
                obj.NguoiCapNhatLookup = new NguoiSuDungDao().GetById(userInfo.ID);

                return obj;
            }
        }

        public System.Collections.Generic.IList<CMS.Core.Domain.BaiVietTheLoai> TheLoaiList
        {
            get
            {
                return this._TheLoaiList;
            }
            set
            {
                this._TheLoaiList = value;
            }
        }

        #endregion
    }
}